home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / adv_lighting / spotLight.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.3 KB  |  294 lines

  1. /* Copyright 1993, 1995, 1996, Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16. /* spotLight.c - spotlight shining on a stack of flat grids
  17.  *
  18.  *    Escape key            - exit the program
  19.  *    Left Mouse Button        - change incidence and azimuth angles
  20.  *    <d> key                - toggle depth buffering
  21.  *    <s> key                - toggle flat/smooth grid
  22.  *     <R> Key                - reset viewpoint
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <math.h>
  29. #include <stdio.h>
  30.  
  31. /* Function Prototypes */
  32.  
  33. GLvoid  initgfx( GLvoid );
  34. GLvoid  visibility( GLint );
  35. GLvoid  animate( GLvoid );
  36. GLvoid  drawScene( GLvoid );
  37. GLvoid  reshape( GLsizei, GLsizei );
  38. GLvoid  keyboard( GLubyte, GLint, GLint );
  39. GLvoid  mouse( GLint, GLint, GLint, GLint );
  40. GLvoid  motion( GLint, GLint );
  41.  
  42. GLvoid  toggleDepthBuffer( GLvoid );
  43. GLvoid  toggleSmooth( GLvoid );
  44.  
  45. void resetView( GLvoid );
  46. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  47. void printHelp( char * );
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. /* Global Variables */
  54.  
  55. static GLfloat        spin = 0.0;
  56.  
  57. static GLboolean    moving = GL_FALSE;
  58.  
  59. static GLdouble        xStart = 0.0, yStart = 0.0;
  60.  
  61. static GLdouble        fovy, near, far; 
  62. static GLfloat        distance, twistAngle, incAngle, azimAngle;
  63.  
  64. void
  65. main( int argc, char *argv[] )
  66. {
  67.     GLsizei width, height;
  68.  
  69.     glutInit( &argc, argv );
  70.  
  71.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  72.     height = glutGet( GLUT_SCREEN_HEIGHT );
  73.     glutInitWindowPosition( 0, height / 4 );
  74.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  75.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  76.     glutCreateWindow( argv[0] );
  77.  
  78.     initgfx();
  79.  
  80.     glutMouseFunc( mouse );
  81.     glutMotionFunc( motion );
  82.     glutKeyboardFunc( keyboard );
  83.     glutIdleFunc( animate );
  84.     glutVisibilityFunc( visibility );
  85.     glutReshapeFunc( reshape );
  86.     glutDisplayFunc( drawScene );
  87.  
  88.     printHelp( argv[0] );
  89.  
  90.     glutMainLoop();
  91. }
  92.  
  93. void
  94. printHelp( char *progname )
  95. {
  96.     fprintf(stdout, "\n%s - demonstrate spot lights\n\n"
  97.         "Escape key            - exit the program\n"
  98.         "Left Mousebutton        - move eye position\n"
  99.         "<d> key            - toggle depth buffering\n"
  100.         "<s> key            - toggle flat/smooth grid\n" 
  101.         "<R> key            - reset viewpoint \n\n", 
  102.         progname);
  103. }
  104.  
  105. GLvoid
  106. initgfx( GLvoid )
  107. {
  108.     GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  109.  
  110.     glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse );
  111.  
  112.     glEnable( GL_LIGHTING );
  113.     glEnable( GL_LIGHT0 );
  114.  
  115.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  116.  
  117.     glEnable( GL_DEPTH_TEST );
  118.  
  119.     fovy = 60.0;    /* field of view in Y */
  120.     near = 3.0;    /* Near clipping plane location */
  121.     far  = 12.0;    /* Far clipping plane location */
  122.  
  123.     resetView();
  124. }
  125.  
  126. GLvoid
  127. reshape( GLsizei width, GLsizei height )
  128. {
  129.     GLdouble    aspect;
  130.  
  131.     glViewport( 0, 0, width, height );
  132.  
  133.     glMatrixMode( GL_PROJECTION );
  134.     glLoadIdentity();
  135.     aspect = (GLdouble) width / (GLdouble) height;
  136.     gluPerspective( fovy, aspect, near, far );
  137.     glMatrixMode( GL_MODELVIEW );
  138. }
  139.  
  140. GLvoid 
  141. animate( GLvoid )
  142. {
  143.     /* update the rotation of the spotlight */
  144.     spin = fmodf( (spin + 3.0), 360.0 );
  145.  
  146.     /* Tell GLUT to redraw the scene */
  147.     glutPostRedisplay();
  148. }
  149.  
  150. GLvoid
  151. visibility( int state ) 
  152. {
  153.     if (state == GLUT_VISIBLE) {
  154.         glutIdleFunc( animate );
  155.     } else {
  156.         glutIdleFunc( NULL );
  157.     }
  158. }
  159.  
  160. GLvoid 
  161. keyboard( GLubyte key, GLint x, GLint y )
  162. {
  163.     switch (key) {
  164.     case 'd':    /* toggle depth buffering */
  165.         toggleDepthBuffer();
  166.         glutPostRedisplay();
  167.         break;
  168.     case 's':
  169.         toggleSmooth();
  170.         glutPostRedisplay();
  171.         break;
  172.     case 'R':
  173.         resetView();
  174.         glutPostRedisplay();
  175.         break;
  176.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  177.         exit(0);
  178.     }
  179. }
  180.  
  181. GLvoid 
  182. mouse( GLint button, GLint state, GLint x, GLint y )
  183. {
  184.     if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) {
  185.         /* Update the saved mouse position */
  186.         xStart = x;
  187.         yStart = y;
  188.         moving = GL_TRUE;
  189.     } else {
  190.         moving = GL_FALSE;
  191.     }
  192. }
  193.  
  194. GLvoid
  195. motion( GLint x, GLint y )
  196. {
  197.     if (moving) {
  198.         /* Adjust the eye position based on the mouse position */
  199.         azimAngle += (GLdouble) (x - xStart);
  200.         incAngle -= (GLdouble) (y - yStart);
  201.     
  202.         /* Update the stored mouse position for later use */
  203.         xStart = x;
  204.         yStart = y;
  205.  
  206.         glutPostRedisplay();
  207.     }
  208. }
  209.  
  210. void
  211. resetView()
  212. {
  213.     distance = 4.5;
  214.     incAngle = -25.0;
  215.     azimAngle = 0.0;
  216. }
  217.  
  218. void
  219. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence, 
  220.             GLfloat twist)
  221. {
  222.     glTranslatef( 0.0, 0.0, -distance);
  223.     glRotatef( -twist, 0.0, 0.0, 1.0);
  224.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  225.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  226. }
  227.  
  228. GLvoid
  229. toggleSmooth( GLvoid )
  230. {
  231.     static GLboolean drawSmooth = GL_TRUE;
  232.  
  233.     drawSmooth = !drawSmooth;
  234.     if (drawSmooth) {
  235.         glShadeModel( GL_SMOOTH );
  236.     } else {
  237.         glShadeModel( GL_FLAT );
  238.     }
  239. }
  240.  
  241. GLvoid  
  242. toggleDepthBuffer( GLvoid )
  243. {
  244.     static GLboolean depthFlag = GL_TRUE;
  245.  
  246.     depthFlag = !depthFlag;
  247.     if (depthFlag) {
  248.         glEnable( GL_DEPTH_TEST );
  249.         printf("Depth test ON\n");
  250.     } else {
  251.         glDisable( GL_DEPTH_TEST );
  252.         printf("Depth test OFF\n");
  253.     }
  254. }
  255.  
  256. GLvoid
  257. drawScene( GLvoid )
  258. {
  259.     int i;
  260.  
  261.     GLfloat spot_dir[] = { 0.2, -1.0, 0.0 };
  262.     GLfloat spot_cutoff = 10.0;
  263.     GLfloat spot_exp = 128.0;
  264.  
  265.     /* Note that a spotlight is a local light */
  266.     GLfloat light_position[] = { 0.0, 2.0, 0.0, 1.0 };
  267.  
  268.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  269.  
  270.     glPushMatrix();
  271.         polarView( distance, azimAngle, incAngle, 0 );
  272.  
  273.         glPushMatrix();
  274.             /* make the spotlight rotate */
  275.             glRotatef( spin, 0.0, 1.0, 0.0 );
  276.             glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spot_dir );
  277.             glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, spot_cutoff );
  278.             glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, spot_exp );
  279.             glLightfv( GL_LIGHT0, GL_POSITION, light_position );
  280.         glPopMatrix();
  281.  
  282.         for( i = -2; i < 2; i++ ) {
  283.             glPushMatrix();
  284.                 glTranslatef( 0.0, i * 0.5, 0.0 );
  285.                 glRotatef( -90.0, 1.0, 0.0, 0.0 );
  286.                 SolidGrid();
  287.             glPopMatrix();
  288.         }
  289.     glPopMatrix();
  290.  
  291.     glutSwapBuffers();
  292. }
  293.  
  294.